/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.parts.impl;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent;
import org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.api.parts.ISWTPropertiesEditionPart;
import org.eclipse.emf.eef.runtime.impl.notify.PropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.impl.parts.CompositePropertiesEditionPart;
import org.eclipse.emf.eef.runtime.ui.parts.PartComposer;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.BindingCompositionSequence;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.CompositionSequence;
import org.eclipse.emf.eef.runtime.ui.utils.EditingUtils;
import org.eclipse.emf.eef.runtime.ui.widgets.referencestable.ReferencesTableContentProvider;
import org.eclipse.emf.eef.runtime.ui.widgets.referencestable.ReferencesTableSettings;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TableColumn;
import org.omg.deployment.Any;
import org.omg.deployment.Property;
import org.omg.deployment.parts.ConfigPropertiesPropertiesEditionPart;
import org.omg.deployment.parts.DeploymentViewsRepository;
import org.omg.deployment.providers.DeploymentMessages;


/**
 * @author Toby McClean; Zeligsoft (2009) Limited
 * @generated
 */
public class ConfigPropertiesPropertiesEditionPartImpl extends CompositePropertiesEditionPart implements ISWTPropertiesEditionPart, ConfigPropertiesPropertiesEditionPart {

	/**
	 * @generated
	 */
	protected TableViewer properties;

	/**
	 * @generated
	 */
	protected List<ViewerFilter> propertiesBusinessFilters = new ArrayList<ViewerFilter>();

	/**
	 * @generated
	 */
	protected List<ViewerFilter> propertiesFilters = new ArrayList<ViewerFilter>();



	/**
	 * Default constructor
	 * @param editionComponent the {@link IPropertiesEditionComponent} that manage this part
	 * @generated
	 */
	public ConfigPropertiesPropertiesEditionPartImpl(IPropertiesEditionComponent editionComponent) {
    super(editionComponent);
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.ISWTPropertiesEditionPart#
	 * 			createFigure(org.eclipse.swt.widgets.Composite)
	 * @generated
	 */
	public Composite createFigure(final Composite parent) {
    view = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;
    view.setLayout(layout);
    createControls(view);
    return view;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.ISWTPropertiesEditionPart#
	 * 			createControls(org.eclipse.swt.widgets.Composite)
	 * @generated
	 */
	public void createControls(Composite view) { 
    CompositionSequence configPropertiesStep = new BindingCompositionSequence(propertiesEditionComponent);
    configPropertiesStep.addStep(DeploymentViewsRepository.ConfigProperties.properties);
    
    composer = new PartComposer(configPropertiesStep) {

      @Override
      public Composite addToPart(Composite parent, Object key) {
        if (key == DeploymentViewsRepository.ConfigProperties.properties) {
          return createPropertiesTableComposition(parent);
        }
        return parent;
      }
    };
    composer.compose(view);
  }

	/**
   * @param container
   * @generated NOT
   */
  protected Composite createPropertiesTableComposition(Composite container) {
    Composite tableContainer = new Composite(container, SWT.NONE);
    GridLayout tableContainerLayout = new GridLayout();
    GridData tableContainerData = new GridData(GridData.FILL_BOTH);
    tableContainerData.horizontalSpan = 3;
    tableContainer.setLayoutData(tableContainerData);
    tableContainerLayout.numColumns = 2;
    tableContainer.setLayout(tableContainerLayout);
    org.eclipse.swt.widgets.Table tableProperties = new org.eclipse.swt.widgets.Table(tableContainer, SWT.FULL_SELECTION);
    tableProperties.setHeaderVisible(true);
    GridData gdProperties = new GridData();
    gdProperties.grabExcessHorizontalSpace = true;
    gdProperties.horizontalAlignment = GridData.FILL;
    gdProperties.grabExcessVerticalSpace = true;
    gdProperties.verticalAlignment = GridData.FILL;
    tableProperties.setLayoutData(gdProperties);
    tableProperties.setLinesVisible(true);

        TableColumn name = new TableColumn(tableProperties, SWT.NONE);
        name.setWidth(80);
        name.setText("Name"); //$NON-NLS-1$
        
        TableColumn value = new TableColumn(tableProperties, SWT.NONE);
        value.setWidth(80);
        value.setText("Value"); //$NON-NLS-1$

    properties = new TableViewer(tableProperties);
    properties.setContentProvider(new ArrayContentProvider());
    properties.setLabelProvider(new ITableLabelProvider() {
            public String getColumnText(Object object, int columnIndex) {
              if (object instanceof Property) {
                Property property = (Property) object;
            	  switch (columnIndex) {
                case 0:
                  return property.getName();
                case 1:
                	Any value = property.getValue();
                	if(value != null) {
                		return value.toString();
                	} else {
                		return "";
                	}
                }
              }
              return ""; //$NON-NLS-1$
            }
      
            public Image getColumnImage(Object element, int columnIndex) {
              return null;
            }
      

      public void addListener(ILabelProviderListener listener) {
      }

      public void dispose() {
      }

      public boolean isLabelProperty(Object element, String property) {
        return false;
      }

      public void removeListener(ILabelProviderListener listener) {
      }

    });
    properties.getTable().addListener(SWT.MouseDoubleClick, new Listener(){

      public void handleEvent(Event event) {
        if (properties.getSelection() instanceof IStructuredSelection) {
          IStructuredSelection selection = (IStructuredSelection) properties.getSelection();
          if (selection.getFirstElement() instanceof EObject) {
            propertiesEditionComponent.firePropertiesChanged(new PropertiesEditionEvent(ConfigPropertiesPropertiesEditionPartImpl.this, DeploymentViewsRepository.ConfigProperties.properties, PropertiesEditionEvent.COMMIT, PropertiesEditionEvent.EDIT, null, selection.getFirstElement()));
            properties.refresh();
          }
        }
      }

    });
    GridData propertiesData = new GridData(GridData.FILL_HORIZONTAL);
    propertiesData.minimumHeight = 120;
    propertiesData.heightHint = 120;
    properties.getTable().setLayoutData(propertiesData);
    for (ViewerFilter filter : this.propertiesFilters) {
      properties.addFilter(filter);
    }
    EditingUtils.setID(properties.getTable(), DeploymentViewsRepository.ConfigProperties.properties);
    EditingUtils.setEEFtype(properties.getTable(), "eef::TableComposition::field"); //$NON-NLS-1$
    createPropertiesPanel(tableContainer);
    return container;
  }

  /**
   * @param container
   * @generated NOT
   */
  protected Composite createPropertiesPanel(Composite container) {
    Composite propertiesPanel = new Composite(container, SWT.NONE);
    GridLayout propertiesPanelLayout = new GridLayout();
    propertiesPanelLayout.numColumns = 1;
    propertiesPanel.setLayout(propertiesPanelLayout);
    return propertiesPanel;
  }

  /**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionListener#firePropertiesChanged(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public void firePropertiesChanged(IPropertiesEditionEvent event) {
  }



	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.ConfigPropertiesPropertiesEditionPart#initProperties(EObject current, EReference containingFeature, EReference feature)
	 * @generated
	 */
	public void initProperties(ReferencesTableSettings settings) {
    if (current.eResource() != null && current.eResource().getResourceSet() != null)
      this.resourceSet = current.eResource().getResourceSet();
    ReferencesTableContentProvider contentProvider = new ReferencesTableContentProvider();
    properties.setContentProvider(contentProvider);
    properties.setInput(settings);
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.ConfigPropertiesPropertiesEditionPart#updateProperties()
	 * @generated
	 */
	public void updateProperties() {
  properties.refresh();
}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.ConfigPropertiesPropertiesEditionPart#addFilterProperties(ViewerFilter filter)
	 * @generated
	 */
	public void addFilterToProperties(ViewerFilter filter) {
    propertiesFilters.add(filter);
    if (this.properties != null) {
      this.properties.addFilter(filter);
    }
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.ConfigPropertiesPropertiesEditionPart#addBusinessFilterProperties(ViewerFilter filter)
	 * @generated
	 */
	public void addBusinessFilterToProperties(ViewerFilter filter) {
    propertiesBusinessFilters.add(filter);
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.ConfigPropertiesPropertiesEditionPart#isContainedInPropertiesTable(EObject element)
	 * @generated
	 */
	public boolean isContainedInPropertiesTable(EObject element) {
    return ((ReferencesTableSettings)properties.getInput()).contains(element);
  }







	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.emf.eef.runtime.api.parts.IPropertiesEditionPart#getTitle()
	 * @generated
	 */
	public String getTitle() {
    return DeploymentMessages.ConfigProperties_Part_Title;
  }



}
